package com.blocks.gui;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

import com.blocks.ScriptBlocksApplication;
import com.blocks.blocks.Block;
import com.blocks.controller.BlockUtils;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.ListBox;
import de.lessvoid.nifty.controls.ListBoxSelectionChangedEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

//de.lessvoid.nifty.examples.controls.ControlsDemoStartScreen
public class InventoryScreenController implements ScreenController {
	private Nifty nifty;
	private Screen screen;
	private ListBox dropDown1;
	private ListBox dropDownModel;
	private ListBox dropDownGroup;

	@Override
	public void bind(Nifty nifty, Screen screen) {
		this.nifty = nifty;
		this.screen = screen;
		this.dropDown1 = findDropDownControl("dropDown1");
		dropDownModel = findDropDownControl("dropDownModel");
		dropDownGroup = findDropDownControl("dropDownGroup");

		if (dropDown1 != null) {
			Map<Long, Block> blockTypeMap = ScriptBlocksApplication
					.getInstance().getBlockTypeMap();
			ValueComparator valueComparator = new ValueComparator(blockTypeMap);
			TreeMap<Long, Block> sortedMap = new TreeMap<Long, Block>(valueComparator);
			sortedMap.putAll(blockTypeMap);
			for (Block block : sortedMap.values()) {
				if (BlockListGroup.LOGIC.equals(block.getBlockListGroup())) {
					dropDown1.addItem(block.getName());
				} else if (BlockListGroup.FREQUENCY.equals(block
						.getBlockListGroup())) {
					dropDownModel.addItem(block.getName());
				} else if (BlockListGroup.GAME.equals(block
						.getBlockListGroup())) {
					dropDownGroup.addItem(block.getName());
				}
			}
		}
	}

	@Override
	public void onEndScreen() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStartScreen() {
		// TODO Auto-generated method stub

	}

	public void blockSelected(String dropDownName) {
		ListBox dropDown1 = findDropDownControl(dropDownName);
		ScriptBlocksApplication.getInstance().setHudBlockIndex((String)
				dropDown1.getSelection().get(0));
		BlockUtils.updateHUDBlock();
		ScriptBlocksApplication.getInstance().gameState();
		ScriptBlocksApplication.getInstance().setNiftyActive(false);
		nifty.exit();
	}

	public void backToGame() {
		ListBox dropDown1 = findDropDownControl("dropDown1");

		ScriptBlocksApplication.getInstance().setHudBlockIndex(
				dropDown1.getSelection().toString());
		BlockUtils.updateHUDBlock();
		ScriptBlocksApplication.getInstance().gameState();
		ScriptBlocksApplication.getInstance().setNiftyActive(false);
		nifty.exit();
	}

	private ListBox findDropDownControl(final String id) {
		return screen.findNiftyControl(id, ListBox.class);
	}

	// @NiftyEventSubscriber(id="dropDown1")
	@NiftyEventSubscriber(pattern = "dropDown.*")
	public void onListBoxSelectionChanged(final String id,
			final ListBoxSelectionChangedEvent<String> changed) {
		blockSelected(id);
	}
	
	class ValueComparator implements Comparator {

		  Map base;
		  public ValueComparator(Map base) {
		      this.base = base;
		  }

		  public int compare(Object a, Object b) {
			String nameA = ((Block)base.get(a)).getName();
			String nameB = ((Block)base.get(b)).getName();
		    if(nameA.compareTo(nameB)>0) {
		      return 1;
		    } else if(nameA.compareTo(nameB)==0) {
		      return 0;
		    } else {
		      return -1;
		    }
		  }
		}

}
